package cn.itsource.pethome.pet.service.impl;

import cn.itsource.pethome.Basic.service.Impl.BaseServiceImpl;
import cn.itsource.pethome.pet.domain.Pet;
import cn.itsource.pethome.pet.mapper.PetMapper;
import cn.itsource.pethome.pet.service.IPetService;
import cn.itsource.pethome.user.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    @Autowired
    private PetMapper petMapper;

    /**
     * 上架宠物
     * @param ids
     */
    @Override
    public void onState(List<Long> ids) {
        petMapper.onState(ids);
    }

    /**
     * 下架宠物
     * @param ids
     */
    @Override
    public void downState(List<Long> ids) {
        petMapper.downState(ids);
    }

    /**
     * 通过id查询
     * @param id
     * @return
     */
    @Override
    public Pet findById(Long id) {
        return petMapper.findById(id);
    }

    /**
     * 领养
     * @param id
     * @param loginUser
     */
    @Override
    public void adopt(Long id, User loginUser) {
        //通过id获取宠物
        Pet pet = petMapper.findById(id);
        //设置宠物状态为已领养
        pet.setState(0);
        //设置领养人
        pet.setUser(loginUser);
        //将商铺设置为空
        pet.setShop(null);
        //设置下架时间
        pet.setOffsaletime(new Date());
        //更新宠物列表
        petMapper.update(pet);
    }


}
